<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList">
        <template slot="menuLeft">
          <el-button
            v-if="permissions.support_sw_st_log_set_edit"
            type="primary" size="small"
            @click="handleLogExpireSet">日志保存期限
          </el-button>
          <el-button
            type="primary" size="small"
            v-if="permissions.support_sw_st_operate_log_record"
            @click="handleArchiveLog">归档
          </el-button>
          </template>
		</avue-crud>
		<el-dialog :visible.sync="logSetVisible" title="日志保存期限设置">
		  <avue-form
			v-model="logSetForm"
			:option="formOption"
			@submit="submitLogSet">
			<template slot="menuForm">
			  <el-button size="small" @click="logSetVisible=false">取消</el-button>
			</template>
		  </avue-form>
		</el-dialog>
        </basic-container>
    </div>
</template>

<script>
  import baseCrud from '@/mixins/base-crud'
  import {getLogExpire, setLogExpire, recordBatch} from '@/api/support/swstoperatelog'

  export default {
    name: 'swstoperatelog',
    mixins: [baseCrud('support/swstoperatelog')],
    data() {
      return {
        params: {
          recordFlag: '0'
        },
        logSetVisible: false,
        logSetForm: {},
        formOption: {
          submitText: '确认',
          emptyBtn: false,
          column: [{
            label: '周期',
            prop: 'saveCycle',
            type: 'select',
            span: 24,
            dicUrl: '/admin/dict/type/support_log_save_cycle',
            clearable: false
          }, {
            label: '周期值',
            prop: 'cycleValue',
            type: 'number',
            span: 24,
            rules: [{
              required: true,
              message: "请输入周期值",
              trigger: "blur"
            }, {
              type: 'integer',
              min: 1,
              message: '周期值必须大于0'
            }]
          }]
        }
      }
    },
    computed: {
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions.support_swstoperatelog_add, false),
          delBtn: this.vaildData(this.permissions.support_swstoperatelog_del, false),
          editBtn: this.vaildData(this.permissions.support_swstoperatelog_edit, false)
        };
      }
    },
    methods: {
      handleLogExpireSet() {
        getLogExpire().then(response => {
          this.logSetForm = response.data.data
          this.logSetVisible = true
        })
      },
      submitLogSet(form, done) {
        setLogExpire(form).then(() => {
          done()
          this.logSetVisible = false
        })
      },
      handleArchiveLog() {
        var tableSelect = this.$refs.crud.tableSelect
        if (tableSelect.length == 0) {
          this.$message.info('请选择要归档的记录')
          return
        }
        var ids = tableSelect.map(row => row.id)
        recordBatch(ids).then(response => {
          this.getList(this.page)
        })
      }
    }
  }
</script>
